package com.example.demo.controller;

import com.example.demo.entity.MbtiResult;
import com.example.demo.repository.MbtiResultRepository;
import com.example.demo.repository.StudentRepository;
import com.example.demo.repository.OrganizationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/results")
@CrossOrigin
public class ResultController {

    @Autowired
    private MbtiResultRepository resultRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private OrganizationRepository organizationRepository;

    // 按学生ID查
    @GetMapping("/student/{studentId}")
    public List<MbtiResult> getByStudent(@PathVariable Long studentId) {
        return studentRepository.findById(studentId)
                .map(resultRepository::findByStudent)
                .orElseThrow(() -> new RuntimeException("学生不存在"));
    }

    // 按班级ID查
    @GetMapping("/class/{classId}")
    public List<MbtiResult> getByClass(@PathVariable Long classId) {
        return resultRepository.findByClazz_Id(classId);
    }

    // 后续可以添加：按专业、学院查询（需遍历组织结构）
}
